package com.wuyan.locationdog.ui.device


import android.os.Bundle
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceBeanContent
import com.arcsoft.commonbase.ext.defaultText
import com.arcsoft.commonbase.ext.loadImageCircleCar
import com.arcsoft.commonbase.ext.radius
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.TimeUtils
import com.wuyan.locationdog.databinding.FlowDetailActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel

class FlowDetailActivity : BaseBindingActivity<FlowDetailActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    override fun initData() {
        val deviceBeanContent = DeviceHelp.selectDeviceBeanContent

        BarUtils.transparentStatusBar(this)
        setTitleText("流量套餐")
        binding.apply {
            seekBg.radius(4F)

            imei.text = "设备ID：" + deviceBeanContent.imei
            deviceName.text = DeviceHelp.getDeviceName(deviceBeanContent)
            deviceAvatarUrl.loadImageCircleCar(deviceBeanContent.dog?.dogImg)


            btnSure.setOnClickListener {
                val bundle = Bundle()
                bundle.putInt("commodityId", 999)
                ActivityUtils.startActivity(bundle, ServiceDetailActivity::class.java)
            }
        }


        val map = HashMap<String, Any>()
        map["deviceId"] = deviceBeanContent.deviceId
        mViewModel.deviceDetail(map)
    }


    override fun observe() {
        super.observe()
        mViewModel.deviceBeanContent.observe(this) {
            binding.apply {
                simNo.text =it.deviceDetailVO.simNo
                serviceStartTime.text = it.deviceDetailVO.serviceStartTime.substring(0, 10)
                serviceTime.text = it.deviceDetailVO.serviceTime.substring(0, 10)

                val time1 = TimeUtils.string2Millis(it.deviceDetailVO.serviceStartTime)
                val time2 = TimeUtils.string2Millis(it.deviceDetailVO.serviceTime)
                val nowMills = TimeUtils.getNowMills()

                val gress = (nowMills - time1) * 100 / (time2 - time1)
                progress.progress = gress.toInt()


                val d = (time2 - nowMills) / (24 * 60 * 60 * 1000)
                day.text = d.toString()
            }
        }
    }
}